﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace bttuan7
{
    public partial class Form1 : Form
    {
        public bool dang_nhap_thanh_cong = false;

        public my_users this_user;

        public Form1()
        {
            InitializeComponent();
        }

        private void btn_ok_Click(object sender, EventArgs e)
        {
            if (my_users.authenticating(tb_login_name.Text, tb_login_password.Text))
            {
                dang_nhap_thanh_cong = true;
                this_user = new my_users(tb_login_name.Text);
                this.Close();
            }
            else
            {
                MessageBox.Show("Tài khoản hoặc mật khẩu không hợp lệ");
            }
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }

    public class my_users
    {
        public string user_name;
        public string user_password;
        public string full_name;
        public string group_id;

        static public bool authenticating(string login_username, string login_password)
        {
            if (File.Exists("users/" + login_username + ".txt"))
            {
                StreamReader reader = new StreamReader("users/" + login_username + ".txt");
                string dong1 = reader.ReadLine();
                string dong2 = reader.ReadLine();
                reader.Close();
                if (login_password == dong2)
                    return true;
                return false;
            }
            return false;
        }

        static public void delete(string user_name)
        {
            File.Delete("users/" + user_name + ".txt");
        }

        static public List<my_users> get_users()
        {
            List<my_users> list_user = new List<my_users>();
            DirectoryInfo cur_dir = new DirectoryInfo(Directory.GetCurrentDirectory() + "/users/");
            foreach (FileInfo file in cur_dir.GetFiles())
            {
                StreamReader reader = new StreamReader(file.FullName);
                string username, password, fullname, groupid;
                username = reader.ReadLine(); password = reader.ReadLine(); fullname = reader.ReadLine(); groupid = reader.ReadLine();
                list_user.Add(new my_users(username,
                    password,
                    fullname,
                    groupid));
                reader.Close();
            }
            return list_user;
        }
        public void write_down(string my_users_key)
        {
            StreamWriter writer = new StreamWriter("users/" + user_name + ".txt", false);
            writer.WriteLine(user_name);
            writer.WriteLine(user_password);
            writer.WriteLine(full_name);
            writer.WriteLine(group_id);
            writer.Close();
        }

        public my_users()
        {
            user_password = "";
        }
        public my_users(string username)
        {
            user_name = username;
            StreamReader reader = new StreamReader("users/" + username + ".txt");
            user_name = reader.ReadLine();
            user_password = reader.ReadLine();
            full_name = reader.ReadLine();
            group_id = reader.ReadLine();
            reader.Close();
        }

        public my_users(string username, string password, string fullname, string groupid)
        {
            user_name = username;
            user_password = password;
            full_name = fullname;
            group_id = groupid;
        }
    }
}
